﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
/// <summary>
/// CuongBH - 10/12/2011
/// </summary>
public partial class Administrator_AdminProductImageIU : System.Web.UI.Page
{
    public Int32 HinhAnhID = HangSo.DEFAULT_ID;
    public Int32 sanPhamID = HangSo.DEFAULT_ID;
    private String message = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Message"] != null)
        {
            try
            {
                message = Request.QueryString["Message"].ToString();
            }
            catch (Exception)
            {
                message = "";
            }
        }

        //hiển thị Message nếu có giá trị Message gửi sang từ màn hình nguồn
        this.lbMessage.Text = message;
        this.lbMessage.CssClass = "BlueCorlor";

        try
        {
            HinhAnhID = Int32.Parse(Request.QueryString["HinhAnhID"].ToString());
        }
        catch (Exception)
        {
        }

        try
        {
            sanPhamID = Int32.Parse(Request.QueryString["SanPhamID"].ToString());
        }
        catch (Exception)
        {
            Response.Redirect("~/Administrator/AdminProductImages.aspx?SanPhamID=" + sanPhamID.ToString());
        }

        if (!Page.IsPostBack)
        {
            if (HinhAnhID == -1)
            {
                ViewState["action"] = HangSo.ACTION_ADD;
                this.imgHinhAnh.ImageUrl = @"~\ImageDatas\Products\noImage.gif";
                this.lbAdminProduct.Text = "Thêm mới ảnh sản phẩm";
            }
            else
            {
                HinhAnhSanPham theHinhAnhSanPham = HinhAnhSanPhamProvider.GetHinhAnhSanPhamByHinhAnhID(HinhAnhID);

                if (theHinhAnhSanPham == null)
                    Response.Redirect("AdminCustomers.aspx");
                else
                {
                    this.imgHinhAnh.ImageUrl = @"~\ImageDatas\Products\" + theHinhAnhSanPham.HinhAnhUrl.ToString();
                    this.ckPublish.Checked = theHinhAnhSanPham.Publish;
                    this.txtNumberOfOrder.Text = theHinhAnhSanPham.NumberOfOrder.ToString();

                    ViewState["HinhAnhUrl"] = theHinhAnhSanPham.HinhAnhUrl.ToString();
                    ViewState["HinhAnhID"] = theHinhAnhSanPham.HinhAnhID;
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    this.lbAdminProduct.Text = "Sửa ảnh sản phẩm";
                }
            }
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/AdminProductImages.aspx?SanPhamID=" + sanPhamID.ToString());
    }

    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: kiểm tra dữ liệu nhập
    /// </summary>
    /// <returns>Trạng thái đúng hay sai</returns>
    private Boolean ValidateData()
    {
        this.lbMessage.Text = "";
        this.lbMessage.CssClass = "";

        String message = "";
        Boolean isSuccess = true;
        String extendsion = HangSo.IMAGE_VALIDATION;

        if (this.fuHinhAnhUrl.HasFile && extendsion.IndexOf(Path.GetExtension(fuHinhAnhUrl.PostedFile.FileName)) < 0)
        {
            message += "Hình ảnh không đúng <br />";
            isSuccess = false;
        }

        if (ViewState["action"].ToString().Trim() == HangSo.ACTION_ADD)
        {
            if (!this.fuHinhAnhUrl.HasFile)
            {
                message += "Hãy chọn hình ảnh <br />";
                isSuccess = false;
            }
        }

        if (!isSuccess)
        {
            this.lbMessage.Text = message;
        }

        return isSuccess;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        String action = ViewState["action"].ToString();

        if (!ValidateData())
            return;

        Page.Validate();

        if (Page.IsValid)
        {
            Int32 hinhAnhIDOut = -1;
            HinhAnhSanPham theHinhAnhSanPham = new HinhAnhSanPham();
            theHinhAnhSanPham.SanPhamID = sanPhamID;
            theHinhAnhSanPham.Publish = this.ckPublish.Checked;

            try
            {
                theHinhAnhSanPham.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theHinhAnhSanPham.NumberOfOrder = 1;
            }

            if (this.fuHinhAnhUrl.HasFile)
            {
                String fileName = fuHinhAnhUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuHinhAnhUrl.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Products//" + newFileName);
                theHinhAnhSanPham.HinhAnhUrl = newFileName.Trim();
            }
            else
            {
                if (action == HangSo.ACTION_UPDATE)
                {
                    theHinhAnhSanPham.HinhAnhUrl = ViewState["HinhAnhUrl"].ToString();
                }
                else
                {
                    theHinhAnhSanPham.HinhAnhUrl = "noImage.gif";
                }
            }

            if (action == HangSo.ACTION_ADD)
            {
                theHinhAnhSanPham.HinhAnhID = -1;
                hinhAnhIDOut = HinhAnhSanPhamProvider.IUHinhAnhSanPham(theHinhAnhSanPham, true);
            }
            else
            {
                theHinhAnhSanPham.HinhAnhID = Int32.Parse(ViewState["HinhAnhID"].ToString());
                hinhAnhIDOut = HinhAnhSanPhamProvider.IUHinhAnhSanPham(theHinhAnhSanPham, false);
            }

            Response.Redirect("~/Administrator/AdminProductImages.aspx?SanPhamID=" + sanPhamID.ToString());
        }
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        String action = ViewState["action"].ToString();

        if (!ValidateData())
            return;

        Page.Validate();

        if (Page.IsValid)
        {
            Int32 hinhAnhIDOut = -1;
            HinhAnhSanPham theHinhAnhSanPham = new HinhAnhSanPham();
            theHinhAnhSanPham.SanPhamID = sanPhamID;
            theHinhAnhSanPham.Publish = this.ckPublish.Checked;

            try
            {
                theHinhAnhSanPham.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theHinhAnhSanPham.NumberOfOrder = 1;
            }

            if (this.fuHinhAnhUrl.HasFile)
            {
                String fileName = fuHinhAnhUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuHinhAnhUrl.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Products//" + newFileName);
                theHinhAnhSanPham.HinhAnhUrl = newFileName.Trim();
            }
            else
            {
                if (action == HangSo.ACTION_UPDATE)
                {
                    theHinhAnhSanPham.HinhAnhUrl = ViewState["HinhAnhUrl"].ToString();
                }
                else
                {
                    theHinhAnhSanPham.HinhAnhUrl = "noImage.gif";
                }
            }

            if (action == HangSo.ACTION_ADD)
            {
                theHinhAnhSanPham.HinhAnhID = -1;
                hinhAnhIDOut = HinhAnhSanPhamProvider.IUHinhAnhSanPham(theHinhAnhSanPham, true);
            }
            else
            {
                theHinhAnhSanPham.HinhAnhID = Int32.Parse(ViewState["HinhAnhID"].ToString());
                hinhAnhIDOut = HinhAnhSanPhamProvider.IUHinhAnhSanPham(theHinhAnhSanPham, false);
            }

            Response.Redirect("~/Administrator/AdminProductImageIU.aspx?SanPhamID=" + sanPhamID.ToString() + "&HinhAnhID=" + hinhAnhIDOut.ToString() + "&Message=Lưu thành công");
        }
    }
}
